# exception_handler.py
from rest_framework.views import exception_handler
from utils import custom_exceptions
from utils.response import failure
from utils.tools import get_classes_as_tuple

def custom_exception_handler(exc, context):
	response = exception_handler(exc, context)
	if isinstance(exc, get_classes_as_tuple(custom_exceptions)):
		return failure(exc.msg, exc.code)
	else:
		return failure(
			response.data["detail"],
			custom_exceptions.UnknownErrorException.code)

def my_exception_handler(view_func):
	def wrapped_view(*args, **kwargs):
		try:
			return view_func(*args, **kwargs)
		except Exception as exc:
			if isinstance(exc, get_classes_as_tuple(custom_exceptions)):
				return failure(exc.msg, exc.code)
			else:
				return failure(
					custom_exceptions.UnknownErrorException.msg,
					custom_exceptions.UnknownErrorException.code
				)
	return wrapped_view